class classRoom {
  constructor(address, name, students) {
    this.address = address
    this.name = name
    this.students = students
  }

  entry(student) {
    this.students.push(student)
  }

  [Symbol.iterator]() {
    let index = 0
    return {
      next: () => {
        if (index < this.students.length) {
          return { done: false, value: this.students[index++] }
        } else {
          return { done: true, value: undefined }
        }
      },
      //用于监听迭代器提前终止，必须返回一个对象，否则报错
      return: () => {
        console.log('iterator stopped')
        // return { done: true, value: undefined }
        return { msg: 'stop iterator' }
      },
    }
  }
}

const cr1 = new classRoom('bj', 'computer', ['jack', 'josh', 'lucy'])
cr1.entry('dd')
for (const cr1Element of cr1) {
  // console.log(cr1Element)
  if (cr1Element === 'dd') break
  console.log(cr1Element)
}
